package com.zhixiang.xaec.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.xaec.dao.system.StaffPositionDao;
import com.zhixiang.xaec.entity.system.Position;
import com.zhixiang.xaec.entity.system.StaffPosition;
import com.zhixiang.xaec.service.system.PositionService;
import com.zhixiang.xaec.service.system.StaffPositionService;
@Service
public class StaffPositionServiceImpl extends BaseServiceImpl<StaffPosition, Long>
implements StaffPositionService
{
    @Autowired
    PositionService positionService;
    @Override
    @Resource(type=StaffPositionDao.class)
    public void setBaseDao(BaseDao<StaffPosition, Long> dao)
    {
        setDao(dao);
    }

    @Override
    public String delete(String[] ids)
    {
       for(String id:ids){
           getDao().remove(Long.valueOf(id));
       }
        return null;
    }

    @Override
    public List<TreeObj> treeLoad(String id)
    {
        if(!"".equals(id)&&id!=null){
        List<TreeObj> treelist = new ArrayList<TreeObj>();    
        Position position =  positionService.get(Long.valueOf(id));
        TreeObj tobj= new TreeObj();
        tobj.setId(position.getId().toString());
        tobj.setText(position.getPositionName());
        treelist.add(tobj);
        findchildren(tobj);
        return treelist;
        }
        return null;
    }
    
    private void findchildren(TreeObj treeObj){
        List<Position> dList = positionService.findByParentId(Long.valueOf(treeObj.getId()));
        if(dList != null&&dList.size()>0) {
            List<TreeObj> treelist = new ArrayList<TreeObj>();
            for (int i = 0; i < dList.size(); i++ )
            {
                Position position =  dList.get(i);
                TreeObj tobj= new TreeObj();
                tobj.setId(position.getId().toString());
                tobj.setText(position.getPositionName());
                List<Position> dd = positionService.findByParentId(position.getId());
                if(dd.size()==0){
                    tobj.setLeaf(true);
                }
                treelist.add(tobj);
            }
            treeObj.setChildren(treelist);
            
            for (int i = 0; i < treelist.size(); i++ )
            {
                TreeObj tbj = treelist.get(i);
                findchildren(tbj);
            }
        }else{
            treeObj.setLeaf(true);
        }
    }  
    

}
